<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>12 事件代理</title>
		<style type="text/css">
			* {
				padding: 0;
				margin: 0;
			}

			ul {
				list-style: none;
				overflow: hidden;
				margin-top: 80px;
			}

			ul li {
				float: left;
				width: 100px;
				height: 30px;
				text-align: center;
				line-height: 30px;
				color: #fff;
				background-color: #000;
				margin: 0 10px
			}
		</style>
	</head>
	<body>
		<ul id="box">
			<li>1</li>
			<li>2</li>
			<li>3</li>
			<li>4</li>
			<li>5</li>
		</ul>
		<script type="text/javascript">
			window.onload = function() {
				// 常规方法实现 不能触发未来添加的事件
				// 1.获取标签
				/* var lis = document.getElementsByTagName('li');
				for(var i = 0; i < lis.length; i++){
					lis[i].onmouseover = function (){
						this.style.backgroundColor = 'blue';
					}
					lis[i].onmouseout = function (){
						this.style.backgroundColor = 'black';
					}
				} */
				var box = document.getElementById('box');
				// 给未来的元素绑定事件,使用事件代理
				box.onmouseover = function (e){
					e = e || window.event;
					var target = e.target || e.srcElement;
					target.style.backgroundColor = 'blue';
				}
				box.onmouseout = function (e){
					e = e || window.event;
					var target = e.target || e.srcElement;
					target.style.backgroundColor = 'black';
				}
				
				

				// 模拟未来的事件添加对应的数据  技术: ajax
				setTimeout(function (){
					var item = document.createElement('li');
					item.innerHTML = '6';
					box.appendChild(item);
				},3000);
				
				
			}
		</script>
	</body>
</html>
